package com.elitel.frame.base.component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
//import com.elitel.common.redis.RedisDbIndex;
//import com.elitel.common.redis.service.RedisCacheService;
import com.elitel.frame.base.service.BaseMasterdetailService;
import com.elitel.frame.base.service.CfgExtmasterService;
import com.elitel.frame.business.dao.ext.ExtMasterMapper;
import com.elitel.frame.main.dao.ext.ExtBaseMasterdetailMapper;
import com.elitel.frame.main.dao.ext.ExtCfgExtmasterMapper;
import com.elitel.frame.main.entity.BaseMasterdetail;
import com.elitel.frame.main.entity.CfgExtmaster;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.elitel.frame.main.entity.ext.BaseMasterdetailExt;
import com.elitel.frame.main.entity.vo.ZTreeDataModelVo;

@Component("redisCacheCfgMaster")
public class RedisCacheCfgMaster {

    //    @Autowired
//    private RedisCacheService redisCacheService;
    @Autowired
    private CfgExtmasterService cfgExtmasterService;
    @Autowired
    private ExtCfgExtmasterMapper extCfgExtmasterMapper;
    @Autowired
    private ExtMasterMapper extMasterMapper;
    @Autowired
    private ExtBaseMasterdetailMapper extBaseMasterdetailMapper;
    @Autowired
    private BaseMasterdetailService baseMasterdetailService;
//    @Autowired
//    private RedisConnInfo redisConnInfo;
    @Autowired
    private CacheCollections cacheCollections;

    /**
     * <pre>cfgMsaterRedisCache(跟据服务ID返回主表服务信息)
     * @author：FanWenquan
     * @date：2017年10月25日 下午1:18:47
     * @return</pre>
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     *
     * @see
     */

    public CfgExtmaster cfgMsaterRedisCache(String serviceID) {
//        int xx = RedisDbIndex.value;
//        List<CfgExtmaster> list = new ArrayList<CfgExtmaster>();
//        if (redisConnInfo.checkConnReids()) {
//            String key = "CFG_EXTMASTER";
//            if (redisCacheService.stringExists(key, xx)) {
//                String cfgStr = redisCacheService.getObject(key, xx);
//                list = (List<CfgExtmaster>) JSON.parseArray(cfgStr, CfgExtmaster.class);
//            } else {
//                list = extCfgExtmasterMapper.selectAllExtMaster();
//                String extmaster = JSON.toJSONString(list);
//                redisCacheService.saveObject(key, extmaster, xx);
//            }
//        } else {
//            list = extCfgExtmasterMapper.selectAllExtMaster();
//        }

        CfgExtmaster extcfgMaster = new CfgExtmaster();
        List<CfgExtmaster> cfgMsaters = cacheCollections.getCfgMsaters();
        for (CfgExtmaster ce : cfgMsaters) {
            if (null != serviceID && serviceID.equals(ce.getGuid())) {
                extcfgMaster = ce;
                break;
            }
        }
        return extcfgMaster;
    }

    /**
     * 刷新主从表redis缓存
     */
    @CachePut(cacheNames = CacheConfig.DEFAULT, key = "'CFG_EXTMASTER'")
    public List<CfgExtmaster> freshCfgMasterRedisCache() {
//        if (!redisConnInfo.checkConnReids()) return;
//
//        int xx = RedisDbIndex.value;
//        List<CfgExtmaster> list = extCfgExtmasterMapper.selectAllExtMaster();
//        String key = "CFG_EXTMASTER";
//        String extmaster = JSON.toJSONString(list);
//        redisCacheService.saveObject(key, extmaster, xx);
        List<CfgExtmaster> list = extCfgExtmasterMapper.selectAllExtMaster();
        return list;
    }


//    public PageResponseVo extmasterTableServerList(CfgExtmaster extmaster, String page_sql, String count_sql) {
//        int xx = RedisDbIndex.value;
//        PageResponseVo pageResponse = new PageResponseVo();
//        if (redisConnInfo.checkConnReids()) {
//            String key = "SERVER_CFGEXTMASTER_" + extmaster.getGuid();
//            if (redisCacheService.stringExists(key, xx)) {
//                String cfgStr = redisCacheService.getObject(key, xx);
//                //list = (List<Map>)JSON.parseArray(cfgStr,Map.class);
//                pageResponse = JSON.parseObject(cfgStr, PageResponseVo.class);
//            } else {
//                List<Map> list = extMasterMapper.queryMasterTBtwoList(page_sql);
//                Long count = extMasterMapper.queryMasterTBCount(count_sql);
//                pageResponse.setTotal(count.intValue());
//                pageResponse.setRows(list);
//                String extendStr = JSON.toJSONString(pageResponse);
//                redisCacheService.saveObject(key, extendStr, xx);
//                if (null != extmaster.getCatchtime() && 0 != extmaster.getCatchtime()) {
//                    redisCacheService.setKeyTimeOut(key, extmaster.getCatchtime(), xx);
//                }
//            }
//        } else {
//            List<Map> list = extMasterMapper.queryMasterTBtwoList(page_sql);
//            Long count = extMasterMapper.queryMasterTBCount(count_sql);
//            pageResponse.setTotal(count.intValue());
//            pageResponse.setRows(list);
//        }
//
//        return pageResponse;
//    }

    /**
     * <pre>extmasterTableServerList(主表服务不分页且设置缓存对数据进行缓存)
     * @author：FanWenquan
     * @date：2017年10月25日 下午1:19:32
     * @param extmaster
     * @param page_sql
     * @param count_sql
     * @return</pre>
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     *
     * @see
     */
    public PageResponseVo extmasterTableServerList(CfgExtmaster extmaster, String page_sql, String count_sql) {
        PageResponseVo pageResponse = new PageResponseVo();
        //设置缓存时长
        if (null != extmaster.getCatchtime() && 0 != extmaster.getCatchtime()) {
            Integer catchtime = extmaster.getCatchtime();
            switch (catchtime) {//分钟
                case 5:
                    pageResponse = extmasterTableServerList5M(extmaster.getGuid(), page_sql, count_sql);
                    break;
                case 10:
                    pageResponse = extmasterTableServerList10M(extmaster.getGuid(), page_sql, count_sql);
                    break;
                case 30:
                    pageResponse = extmasterTableServerList30M(extmaster.getGuid(), page_sql, count_sql);
                    break;
                case 60:
                    pageResponse = extmasterTableServerList60M(extmaster.getGuid(), page_sql, count_sql);
                    break;
                case 120:
                    pageResponse = extmasterTableServerList120M(extmaster.getGuid(), page_sql, count_sql);
                    break;
                default:
                    pageResponse = quertPRV(page_sql, count_sql);
            }
        } else {//不缓存，则直接查询获取结果
            pageResponse = quertPRV(page_sql, count_sql);
        }
        return pageResponse;
    }

    @Cacheable(cacheNames = "DEFAULT", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerListDefault(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = "TIMEOUT_5_MINUTE", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerList5M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = "TIMEOUT_10_MINUTE", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerList10M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = "TIMEOUT_30_MINUTE", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerList30M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = "TIMEOUT_60_MINUTE", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerList60M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    @Cacheable(cacheNames = "TIMEOUT_120_MINUTE", key = "'SERVER_CFGEXTMASTER_'+#mguid")
    public PageResponseVo extmasterTableServerList120M(String mguid, String page_sql, String count_sql) {
        PageResponseVo pageResponse = quertPRV(page_sql, count_sql);
        return pageResponse;
    }

    private PageResponseVo quertPRV(String page_sql, String count_sql) {
        PageResponseVo pageResponse = new PageResponseVo();
        List<Map> list = extMasterMapper.queryMasterTBtwoList(page_sql);
        Long count = extMasterMapper.queryMasterTBCount(count_sql);
        pageResponse.setTotal(count.intValue());
        pageResponse.setRows(list);
        return pageResponse;
    }

    /**
     * @param mguid
     * @return BaseMasterdetail集合
     * @Description 通过主表服务ID查询从表信息
     * @author guoyanfei
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     * @see
     */
    @Cacheable(cacheNames = "DEFAULT", key = "'SERVER_BASEMASTERDETAIL_'+#mguid")
    public List<BaseMasterdetailExt> getMasterdetailbyMguid(String mguid) {
//        int xx = RedisDbIndex.value;
//        List<BaseMasterdetailExt> baseMasterdetails = new ArrayList<>();
//        if (redisConnInfo.checkConnReids()) {
//            String key = "SERVER_BASEMASTERDETAIL_" + mguid;
//            if (redisCacheService.stringExists(key, xx)) {
//                String cfgStr = redisCacheService.getObject(key, xx);
//                baseMasterdetails = (List<BaseMasterdetailExt>) JSON.parseArray(cfgStr, BaseMasterdetailExt.class);
//            } else {
//                baseMasterdetails = baseMasterdetailService.getExtMasterdetailBymguid(mguid);
//                String extendStr = JSON.toJSONString(baseMasterdetails);
//                redisCacheService.saveObject(key, extendStr, xx);
//            }
//        } else {
//            baseMasterdetails = baseMasterdetailService.getExtMasterdetailBymguid(mguid);
//        }

        List<BaseMasterdetailExt> baseMasterdetails = baseMasterdetailService.getExtMasterdetailBymguid(mguid);

        return baseMasterdetails;
    }

    /**
     * @param mguid(服务ID)
     * @description 通过主表服务ID刷新从表缓存
     * @author guoyanfei
     * @date 2017/12/4
     * <p>
     * modify by guoyanfei on 2019/07/16
     * 新增验证redis连通性
     * @see
     */
    @CachePut(cacheNames = CacheConfig.DEFAULT, key = "'SERVER_BASEMASTERDETAIL_'+#mguid")
    public List<BaseMasterdetailExt> refreshCfgMasterdetail(String mguid) {
//        if (!redisConnInfo.checkConnReids()) return;
//
//        int xx = RedisDbIndex.value;
//        String key = "SERVER_BASEMASTERDETAIL_" + mguid;
//        List<BaseMasterdetailExt> baseMasterdetails = baseMasterdetailService.getExtMasterdetailBymguid(mguid);
//        String extendStr = JSON.toJSONString(baseMasterdetails);
//        redisCacheService.saveObject(key, extendStr, xx);
        List<BaseMasterdetailExt> baseMasterdetails = baseMasterdetailService.getExtMasterdetailBymguid(mguid);
        return baseMasterdetails;
    }

}
